package com.wo.android.pockybill;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import android.app.Activity;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.wow.wa.bill.serivce.BillService;
import com.wow.wa.bill.serivce.BillServiceImpl;
import com.wow.wa.bill.util.Args;
import com.wow.wa.bill.vo.Card;
import com.wow.wa.ui.CategoryBtn;
import com.wow.wa.ui.bean.UICategory;
import com.wow.wa.util.CommonUtils;

/**
 * 新增信用卡
 * 
 * @author Andy
 * 
 */
public class AddCardActivity extends Activity {

	private static final String TAG = AddCardActivity.class.getSimpleName();
	
	
	private CategoryBtn name;
	private CategoryBtn amount;
	
	private CategoryBtn note;
	private CategoryBtn type;
	

	private BillService service;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_card_view);
		this.getActionBar().setDisplayHomeAsUpEnabled(true);

		this.service = new BillServiceImpl(this);

		this.name = (CategoryBtn) this.findViewById(R.id.name);
		
		this.amount = (CategoryBtn) this.findViewById(R.id.amount);
		this.note = (CategoryBtn) this.findViewById(R.id.note);
		this.type = (CategoryBtn) this.findViewById(R.id.type);
	
		this.name.createEditDialog(
				this.getString(R.string.name), 
				StringUtils.EMPTY, false,
				InputType.TYPE_CLASS_TEXT);
		
		this.amount.createEditDialog(
				this.getString(R.string.allMoney),
				StringUtils.EMPTY,
				false, InputType.TYPE_CLASS_NUMBER);
		
		this.note.createEditDialog(
				this.getString(R.string.note),
				StringUtils.EMPTY, false,
				InputType.TYPE_CLASS_TEXT);
		
		this.type.createCategoryDialog(
				this.getString(R.string.cardType),
				this.getPayType(), true,
				StringUtils.EMPTY);
		
		
		
		this.amount.bindOnClickEvent();
		this.name.bindOnClickEvent();
		this.note.bindOnClickEvent();
		this.type.bindOnClickEvent();
	
		this.name.setText(this.getString(R.string.clickToEnter));
	}

	private List<UICategory> getPayType() {
		final List<UICategory> isPayCategorys = new ArrayList<UICategory>();
		isPayCategorys.add(new UICategory(Args.Common.STRING_ZERO, this
				.getString(R.string.oneTypePay)));
		isPayCategorys.add(new UICategory(Args.Common.STRING_ONE, this
				.getString(R.string.monthTypePay)));
		return isPayCategorys;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.add, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();

		switch (id) {
		case android.R.id.home:

			this.finish();
			break;

		case R.id.action_save:

			try {
				final Card card = new Card();
				card.setName(this.name.getBeanValue());
				
				card.setAmount(this.amount.getBeanValue());
				card.setNote(this.note.getBeanValue());
				card.setType(this.type.getBeanValue());
				card.setSeq("1");
				this.service.insertCard(card, this);
			} catch (Exception e) {
				Log.v(TAG, "addBill error=" + e);
				
			}
			this.setResult(Args.Common.ADD_CARD_EDIT_CODE);
			this.finish();
			break;

		}
		// TODO Auto-generated method stub
		return super.onOptionsItemSelected(item);
	}

}
